package com.fsy.its;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import com.fsy.common.mqtt.EnableMqtt;
import com.fsy.common.redis.EnableRedis;
import com.fsy.its.netty.NettyServer;
import io.netty.channel.ChannelFuture;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@EnableDubbo
@EnableMqtt
@EnableRedis
@SpringBootApplication
@MapperScan("com.fsy.its.dao")
public class ItsApplication implements CommandLineRunner {

    @Autowired
    private NettyServer nettyServer;

    public static void main(String[] args) {
        SpringApplication.run(ItsApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        ChannelFuture[] futures = nettyServer.start();
        Runtime.getRuntime().addShutdownHook(new Thread(){
            @Override
            public void run() {
                nettyServer.destroy();
            }
        });
        //服务端管道关闭的监听器并同步阻塞,直到channel关闭,线程才会往下执行,结束进程
        for( ChannelFuture future : futures ) {
            future.channel().closeFuture().syncUninterruptibly();
        }
    }
}
